package com.example.chesstest;


import android.content.ClipData;
import android.content.Context;
import android.view.DragEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.DragShadowBuilder;
import android.view.View.OnDragListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;

public class SquareAdapter extends BaseAdapter implements OnTouchListener{
	
	private Context context;
	private boolean isWhite = true;
	
	ImageView pieces, squareView ;
	
	LayoutInflater layoutInflater;
	
	Pawn [] pawn = new Pawn[16];
	Rook [] rook = new Rook[4];
	private int position;

    public SquareAdapter(Context c) {
        context = c;
    }

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mThumbRes.length + 62;
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View squareContainerView = convertView;
		this.position = position;
        
		  if ( convertView == null ) {
		    //Inflate the layout
		    layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		    squareContainerView = 
		      layoutInflater.inflate(R.layout.square, null);
		    squareContainerView.
		    setOnDragListener(new PieceOnDragListener(this.context, this));

		    // Background
		    squareView = (ImageView)squareContainerView.findViewById(R.id.square_background);
		    squareView.setImageResource(this.mThumbRes[(position + position/8)%2]);
		    System.out.println(position); 
		    
		    initPieces(position, squareContainerView);
		  }
		  return squareContainerView;
	}

	private Integer[] mThumbRes = {
            R.drawable.gray_square, R.drawable.black_square,
    };
	

	private void initPieces(int position, View squareContainerView) {
		
		//BLACK pieces init.
		if (position == 0) {
	    	pieces = (ImageView)squareContainerView.findViewById(R.id.rookBp1);
	    	pieces.setImageResource(R.drawable.rook_black_128);
	    	pieces.setTag(position);
	    	if(!getTurn())
	    	{
	    		pieces.setOnTouchListener(this);
	    	}
	    }
		
		if (position == 7) {
	        pieces = (ImageView)squareContainerView.findViewById(R.id.rookBp2);
	    	pieces.setImageResource(R.drawable.rook_black_128);
			pieces.setTag(position);
			if(getTurn())
			{
				pieces.setOnTouchListener(this);
			}
	    }
	    
	    if (position == 1) {
	    	pieces = (ImageView)squareContainerView.findViewById(R.id.knightBp1);
	    	pieces.setImageResource(R.drawable.knight_black_128);
	    	pieces.setTag(position);
	    	pieces.setOnTouchListener(this);
	    }
	    if (position == 6) {
	    	pieces = (ImageView)squareContainerView.findViewById(R.id.knightBp2);
	    	pieces.setImageResource(R.drawable.knight_black_128);
	    	pieces.setTag(position);
	    	pieces.setOnTouchListener(this);
	    }
	    
	    if (position == 2) {
	    	pieces = (ImageView)squareContainerView.findViewById(R.id.bishopBp1);
	    	pieces.setImageResource(R.drawable.bishop_black_128);
	    	pieces.setTag(position);
	    	pieces.setOnTouchListener(this);
	    }
	    
	    if (position == 5) {
	    	pieces = (ImageView)squareContainerView.findViewById(R.id.bishopBp2);
	    	pieces.setImageResource(R.drawable.bishop_black_128);
	    	pieces.setTag(position);
	    	pieces.setOnTouchListener(this);
	    }
	    
	    if (position == 3) {
	    	pieces = (ImageView)squareContainerView.findViewById(R.id.queenBp);
	    	pieces.setImageResource(R.drawable.queen_black_128);
	    	pieces.setTag(position);
	    	pieces.setOnTouchListener(this);
	    }
	    
	    if (position == 4) {
	        pieces = (ImageView)squareContainerView.findViewById(R.id.kingBp);
	    	pieces.setImageResource(R.drawable.king_black_128);
	    	pieces.setTag(position);
	    	pieces.setOnTouchListener(this);
	    }
	    
	    
	    if (position== 8) {
	        // Add The piece
	    	  pieces = (ImageView)squareContainerView.findViewById(R.id.pawnBp1);
	    	  pieces.setImageResource(R.drawable.pawn_black_128);
	    	  pieces.setTag(position);
	    	  pieces.setOnTouchListener(this);
	    }
	    if (position == 9) {
	        // Add The piece
	    	  pieces = (ImageView)squareContainerView.findViewById(R.id.pawnBp2);
	    	  pieces.setImageResource(R.drawable.pawn_black_128);
	    	  pieces.setTag(position);
	    	  pieces.setOnTouchListener(this);
	    }
	    if (position == 10) {
	        // Add The piece
	    	  pieces = (ImageView)squareContainerView.findViewById(R.id.pawnBp3);
	    	  pieces.setImageResource(R.drawable.pawn_black_128);
	    	  pieces.setTag(position);
	    	  pieces.setOnTouchListener(this);
	    }
	    if (position == 11) {
	        // Add The piece
	    	  pieces = (ImageView)squareContainerView.findViewById(R.id.pawnBp4);
	    	  pieces.setImageResource(R.drawable.pawn_black_128);
	    	  pieces.setTag(position);
	    	  pieces.setOnTouchListener(this);
	    }
	    if (position == 12) {
	        // Add The piece
	    	  pieces = (ImageView)squareContainerView.findViewById(R.id.pawnBp5);
	    	  pieces.setImageResource(R.drawable.pawn_black_128);
	    	  pieces.setTag(position);
	    	  pieces.setOnTouchListener(this);
	    }
	    if (position == 13) {
	        // Add The piece
	    	  pieces = (ImageView)squareContainerView.findViewById(R.id.pawnBp6);
	    	  pieces.setImageResource(R.drawable.pawn_black_128);
	    	  pieces.setTag(position);
	    	  pieces.setOnTouchListener(this);
	    }
	    if (position == 14) {
	        // Add The piece
	    	  pieces = (ImageView)squareContainerView.findViewById(R.id.pawnBp7);
	    	  pieces.setImageResource(R.drawable.pawn_black_128);
	    	  pieces.setTag(position);
	    	  pieces.setOnTouchListener(this);
	    }
	    if (position == 15){
	        // Add The piece
	    	  pieces = (ImageView)squareContainerView.findViewById(R.id.pawnBp8);
	    	  pieces.setImageResource(R.drawable.pawn_black_128);
	    	  pieces.setTag(position);
	    	  pieces.setOnTouchListener(this);
	    }
	    
	    //BLACK pieces init end.
	    
	    //WHITE pieces init.
	    if (position == 56) {
	        pieces = (ImageView)squareContainerView.findViewById(R.id.rookWp1);
	    	pieces.setImageResource(R.drawable.rook_white_128);
	    	pieces.setTag(position);
	    	pieces.setOnTouchListener(this);
	    }
	    
	    if (position == 63) {
	        pieces = (ImageView)squareContainerView.findViewById(R.id.rookWp2);
	    	pieces.setImageResource(R.drawable.rook_white_128);
	    	pieces.setTag(position);
	    	pieces.setOnTouchListener(this);
	    }
	    
	    if (position == 57) {
	        pieces = (ImageView)squareContainerView.findViewById(R.id.knightWp1);
	    	pieces.setImageResource(R.drawable.knight_white_128);
	    	pieces.setTag(position);
	    	pieces.setOnTouchListener(this);
	    }
	    
	    if (position == 62) {
	        pieces = (ImageView)squareContainerView.findViewById(R.id.knightWp2);
	    	pieces.setImageResource(R.drawable.knight_white_128);
	    	pieces.setTag(position);
	    	pieces.setOnTouchListener(this);
	    }
	    
	    if (position == 58) {
	        pieces = (ImageView)squareContainerView.findViewById(R.id.bishopWp1);
	    	pieces.setImageResource(R.drawable.bishop_white_128);
	    	pieces.setTag(position);
	    	pieces.setOnTouchListener(this);
	    }
	    
	    if (position == 61) {
	        pieces = (ImageView)squareContainerView.findViewById(R.id.bishopWp2);
	    	pieces.setImageResource(R.drawable.bishop_white_128);
	    	pieces.setTag(position);
	    	pieces.setOnTouchListener(this);
	    }
	    
	    if (position == 59) {
	    	pieces = (ImageView)squareContainerView.findViewById(R.id.queenWp);
	    	pieces.setImageResource(R.drawable.queen_white_128);
	    	pieces.setTag(position);
	    	pieces.setOnTouchListener(this);
	    }
	    
	    if (position == 60) {
	    	pieces = (ImageView)squareContainerView.findViewById(R.id.kingWp);
	    	pieces.setImageResource(R.drawable.king_white_128);
	    	pieces.setTag(position);
	    	pieces.setOnTouchListener(this);
	    }
	    
	    if (position == 48) {
	        // Add The piece
	        pieces = (ImageView)squareContainerView.findViewById(R.id.pawnWp1);
	        pieces.setImageResource(R.drawable.pawn_white_128);
	        pieces.setTag(position);
	        pieces.setOnTouchListener(this);
	    }
	    if (position == 49) {
	        // Add The piece
	        pieces = (ImageView)squareContainerView.findViewById(R.id.pawnWp2);
	        pieces.setImageResource(R.drawable.pawn_white_128);
	        pieces.setTag(position);
	        pieces.setOnTouchListener(this);
	    }
	    if (position == 50) {
	        // Add The piece
	        pieces = (ImageView)squareContainerView.findViewById(R.id.pawnWp3);
	        pieces.setImageResource(R.drawable.pawn_white_128);
	        pieces.setTag(position);
	        pieces.setOnTouchListener(this);
	    }
	    if (position == 51) {
	        // Add The piece
	        pieces = (ImageView)squareContainerView.findViewById(R.id.pawnWp4);
	        pieces.setImageResource(R.drawable.pawn_white_128);
	        pieces.setTag(position);
	        pieces.setOnTouchListener(this);
	    }
	    if (position == 52) {
	        // Add The piece
	        pieces = (ImageView)squareContainerView.findViewById(R.id.pawnWp5);
	        pieces.setImageResource(R.drawable.pawn_white_128);
	        pieces.setTag(position);
	        pieces.setOnTouchListener(this);
	    }
	    if (position == 53) {
	        // Add The piece
	        pieces = (ImageView)squareContainerView.findViewById(R.id.pawnWp6);
	        pieces.setImageResource(R.drawable.pawn_white_128);
	        pieces.setTag(position);
	        pieces.setOnTouchListener(this);
	    }
	    if (position == 54) {
	        // Add The piece
	        pieces = (ImageView)squareContainerView.findViewById(R.id.pawnWp7);
	        pieces.setImageResource(R.drawable.pawn_white_128);
	        pieces.setTag(position);
	        pieces.setOnTouchListener(this);
	    }
	    
	    if (position == 55) {
	        // Add The piece
	    	pieces = (ImageView)squareContainerView.findViewById(R.id.pawnWp8);
	        pieces.setImageResource(R.drawable.pawn_white_128);
	        pieces.setTag(position);
	        pieces.setOnTouchListener(this);
	    }
	    //WHITE init end.*/
	}

	public void setTurn(boolean isWhite)
	{
		this.isWhite = isWhite;
	}
	
	public boolean getTurn()
	{
		return isWhite;
	}
	
	public boolean onTouch(View v, MotionEvent event) {
			      final ClipData data = ClipData.newPlainText("position", position+"");
			      final DragShadowBuilder pieceDragShadowBuilder = new DragShadowBuilder(pieces);
			      v.startDrag(data, pieceDragShadowBuilder, v, 0);
			      return true;
	}		
}
